<?php
header('Content-Type:text/javascript');
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR";');
require('../../lib/base.php');
$base=new base(array('web','advertiser'));
$get=$base->get(array('A'=>false,'B'=>false,'C'=>false,'D'=>false,'E'=>false,'F'=>false,'G'=>false,'H'=>false,'M'=>true,'V'=>false,'I'=>true));
$server=$base->server(array('REQUEST_TIME'=>false,'HTTP_CLIENT_IP'=>false,'HTTP_X_FORWARDED_FOR'=>false,'REMOTE_ADDR'=>false));
$ip=$server['HTTP_CLIENT_IP']?$server['HTTP_CLIENT_IP']:($server['HTTP_X_FORWARDED_FOR']?$server['HTTP_X_FORWARDED_FOR']:($server['REMOTE_ADDR']?$server['REMOTE_ADDR']:false));
$data['date']=$server['REQUEST_TIME'];
$data['visitor']=$base->cookie('visitor');
if($data['visitor']===false){
	$data['visitor']=strtoupper(md5($ip.$get['G'].uniqid()));
	$base->cookie('visitor',$data['visitor'],9999999999);
}
$data['track']=substr(str_shuffle(mt_rand(0,9999999999).'ABCDEF'),0,8);
$site=$base->model('site');
$T=$site->GSite(array('where'=>array('domain='=>$get['I'])));
if(empty($T)){
	$data['site']=$site->ISite(array('domain'=>$get['I'],'date'=>$server['REQUEST_TIME']));
}else{
	$data['site']=reset($T);$data['site']=$data['site']['id'];
}
$T=explode(',',$get['M']);
$data['billboard']=intval($T[0]);$data['format']=intval($T[1]);$data['usage']=intval($T[2]);$data['modality']=intval($T[3]);
$data['version']=intval($get['D']);
/***********************取得当前IP地址ID******************************/
$city=0;$ip=ip2long($ip);$find=pack('N',$ip);$china=file_get_contents($base->vars('sys','root').$base->vars('app','init','ip'));
$front=0;$count=strlen($china)/6;
while($front<$count-1){
	$i=ceil(($front+$count)/2);$start=strcmp($find,substr($china,$i*6,4));
	if($start<0){$count=$i;}else{
		$end=strcmp($find,substr($china,($i+1)*6,4));
		if($end>=0){
			$front=$i;
		}else{
			$city=unpack('C2',substr($china,$i*6+4,2));
			$city=$city[1]*256+$city[2];
			break;
		}
	}
}unset($china);
if($city==0){
	$data['province']=0;$data['city']=0;
}else{
	$city=$base->model('sort')->GSort(array('where'=>array('id='=>$city)));
	if(empty($city)){
		$data['province']=0;$data['city']=0;
	}else{
		$city=reset($city);
		if($city['parent']==0){
			$data['province']=$city['id'];$data['city']=0;
		}else{
			$data['province']=$city['parent'];$data['city']=$city['id'];
		}
	}
}
$ic=unserialize(file_get_contents('ic.d'));
$data['browser']=intval($ic['label'][$get['C']]);$data['lang']=intval($ic['label'][$get['E']]);$data['system']=intval($ic['label'][$get['F']]);$data['screen']=intval($get['G']);
$ad=$ic['ad'][$T[1].':'.$T[3]];
if(empty($ad)){
		echo 'alert("广告位对应广告为空.");';
}else{
	if(empty($get['H'])){
		$data['keyword']='';
	}else{
		$get['H']=urldecode($get['H']);
		if(1==preg_match('/^([\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})*\z/x',$get['H'])){
			$get['H']=iconv('gb2312','UTF-8',$get['H']);
		}
		$user[]='(\d+):'.$get['H'];
		$data['keyword']=$get['H'];
	}
	if(count($ad)>1){//智能投放
		$user[]='(\d+):'.$city;$adcolumn=explode('-',$T[4]);
		foreach($adcolumn as $value){$user[]='(\d+):'.$value;}
		$user=implode('|',$user);$aptitude=array();
		foreach($ad as $key=>$value){
			$matches=array();$aptitude[$key]=0;
			preg_match_all('/'.$user.'/u',$value['tactic'],$matches,PREG_PATTERN_ORDER);
			if(isset($matches[0])){
				foreach($matches[0] as $v){
					$aptitude[$key]+=intval($v);
				}
			}
		}
		array_multisort($aptitude,SORT_DESC,$ad);
	}
	$ad=reset($ad);$data['format']=$ad['format'];$data['advertisers']=$ad['id'];
	$script=$ad['script'];
	echo $script['func']."('{$script['size']}','{$script['format']}','{$ic['ad']['material']}{$script['material']}','{$ic['ad']['server']}mysql.track.php?url={$ad['link']}&action=click&track={$data['track']}&pt={$data['date']}','{$get['V']}');";
	$base->model('data')->insert('pageview',$data);
}
?>